<?php
/**
 * Action Helper for finding days in a month
 */
class Home_Helper_DiaIntervalo extends Zend_Controller_Action_Helper_Abstract
{
	/**
	 * @var Zend_Loader_PluginLoader
	 */
	public $pluginLoader;

	/**
	 * Constructor: initialize plugin loader
	 *
	 * @return void
	 */
	public function __construct()
	{
		$this->pluginLoader = new Zend_Loader_PluginLoader();
	}


	/**
	 * Strategy pattern: call helper as broker method
	 *
	 * @param  int $month
	 * @param  int $year
	 * @return int
	 */
	public function direct($data1, $data2)
	{
		return $this->diferenca($data1,$data2);
	}
	
	function diferenca($data1,$data2)
	{
	
		$neg = false;
	
		$data1 = $this->data2banco($data1);
		$data2 = $this->data2banco($data2);
		if($data1 >= $data2)
		{
			$neg = true;
			$data3 = $data1;
			$data1 = $data2;
			$data2 = $data3;
		}
	
		$datetime1 = new DateTime($data1);
		$datetime2 = new DateTime($data2);
		$interval = $datetime1->diff($datetime2);
		return $interval->format('%a');
	}
	
	
	
	
	public function data2banco ($d2b) {
	
		if(!empty($d2b)){
			$d2b_ano=substr($d2b,6,4);
			$d2b_mes=substr($d2b,3,2);
			$d2b_dia=substr($d2b,0,2);
			$d2b="$d2b_ano-$d2b_mes-$d2b_dia";
		}
		return $d2b;
	}
	
	public function banco2data($b2d) {
	
		if($b2d=="0000-00-00" or empty($b2d))
		{
			$b2d="";
			return $b2d;
		}
		else
		{
			$b2d_ano=substr($b2d,0,4);
			$b2d_mes=substr($b2d,5,2);
			$b2d_dia=substr($b2d,8,2);
			$b2d=$b2d_dia.'-'.$b2d_mes.'-'.$b2d_ano;
			return $b2d;
		}
	}
}